package com.example.teamtribe.common.userinfo;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.teamtribe.Application;
import com.example.teamtribe.R;
import com.example.teamtribe.base.BaseActivity;
import com.example.teamtribe.common.send.SendActivity;
import com.example.teamtribe.database.TeamRequest;
import com.example.teamtribe.database.User;
import com.example.teamtribe.json.JsonTeamRequestReason;
import com.example.teamtribe.utils.SchoolStore;
import com.example.teamtribe.utils.Utility;
import com.google.gson.Gson;

import java.util.List;

import butterknife.BindView;

public class UserInfoActivity extends BaseActivity implements View.OnClickListener, UserInfoContract.View {

    private AlertDialog dialog;
    private ImageView ivDelete;
    private ImageView ivHead;
    private Toolbar tvNickname;
    private TextView tvSchool;
    private TextView tvNo;
    private TextView tvName;
    private TextView tvGender;
    private TextView tvBirthday;
    private TextView tvAcademy;
    private TextView tvMajor;
    private TextView tvGrade;
    private TextView tvClass;
    private Button btSend;
    @BindView(R.id.ll_agreement)
    LinearLayout llAgreement;
    @BindView(R.id.bt_info)
    Button btResponse;
    private UserInfoContract.Presenter presenter;
    private User user;
    private TeamRequest request;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);

        dialog = new AlertDialog.Builder(this)
                .setTitle("警告")
                .setMessage("确定删除此好友？")
                .setNegativeButton("取消", null)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        presenter.delete(user.getRealId());
                    }
                })
                .create();
        ivDelete = findViewById(R.id.iv_more);
        Glide.with(this).load(R.drawable.ic_delete).into(ivDelete);
        ivHead = findViewById(R.id.iv_head);
        tvNickname = findViewById(R.id.tv_nickname);
        tvSchool = findViewById(R.id.tv_school);
        tvNo = findViewById(R.id.tv_no);
        tvName = findViewById(R.id.tv_name);
        tvGender = findViewById(R.id.tv_gender);
        tvBirthday = findViewById(R.id.tv_birthday);
        tvAcademy = findViewById(R.id.tv_academy);
        tvMajor = findViewById(R.id.tv_major);
        tvGrade = findViewById(R.id.tv_grade);
        tvClass = findViewById(R.id.tv_cla);
        btSend = findViewById(R.id.bt_send);
        presenter = new UserInfoPresenter(this);
        user = getIntent().getParcelableExtra("user");
        request = getIntent().getParcelableExtra("request");

        ivDelete.setOnClickListener(this);
        btSend.setOnClickListener(this);
        findViewById(R.id.bt_agree).setOnClickListener(this);
        findViewById(R.id.bt_disagree).setOnClickListener(this);

        ((TextView) findViewById(R.id.tv_title)).setText("用户信息");
        if (request == null) {
            setUserInfo(user);
            setPrivateInfo(user.getIsPublicArray());
            setFriendButton(user.getRealId());
        } else {
            setRequestInfo(request.getRequestReason());
            setResultInfo(request.getResult());
        }

        CollapsingToolbarLayout collapsingToolbarLayout = findViewById(R.id.ctl);
        collapsingToolbarLayout.setCollapsedTitleTextColor(getResources().getColor(R.color.colorAccent));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_more:
                dialog.show();
                break;

            case R.id.bt_send:
                Intent intent = new Intent(UserInfoActivity.this, SendActivity.class);
                intent.putExtra("user", user);
                startActivity(intent);
                break;

            case R.id.bt_agree:
                presenter.response(request, true);
                break;

            case R.id.bt_disagree:
                presenter.response(request, false);
                break;

            default:
                break;
        }
    }

    @Override
    public void next() {
        User myself = Utility.getUser();
        List<Integer> friends = myself.getFriends();
        friends.remove(Integer.valueOf(user.getRealId()));//删除好友
        myself.setFriends(friends);
        Utility.setUser(myself);//先在本地删掉快些，再在服务器获取二次验证
        Application.getBinder().getFriends();
        finish();
    }

    @Override
    public void setResultInfo(final int result) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                btSend.setVisibility(View.GONE);
                switch (result) {
                    case 1:
                        llAgreement.setVisibility(View.GONE);
                        btResponse.setText("已同意");
                        btResponse.setVisibility(View.VISIBLE);
                        break;
                    case 2:
                        llAgreement.setVisibility(View.GONE);
                        btResponse.setText("已拒绝");
                        btResponse.setVisibility(View.VISIBLE);
                        break;
                    default:
                        llAgreement.setVisibility(View.VISIBLE);
                        btResponse.setVisibility(View.GONE);
                        break;
                }
            }
        });
    }

    private void setUserInfo(User user) {
        Glide.with(this).load(Utility.getUri(user.getPortrait())).into(ivHead);
        tvNickname.setTitle(user.getNickname());
        tvSchool.setText(SchoolStore.getSchoolNameById(user.getSchoolId()));
        tvNo.setText(user.getNo());
        tvName.setText(user.getName());
        if (user.getGender() == 1)
            tvGender.setText("男");
        else
            tvGender.setText("女");
        tvBirthday.setText(user.getBirthday());
        tvAcademy.setText(user.getAcademy());
        tvMajor.setText(user.getMajor());
        tvGrade.setText(user.getGrade() + "");
        tvClass.setText(user.getCla() + "班");
    }

    private void setFriendButton(int userId) {
        User myself = Utility.getUser();
        if (myself.getRealId() == userId) {
            btSend.setVisibility(View.GONE);
            return;
        }
        List<Integer> friends = myself.getFriends();
        if (friends == null) {
            return;
        }
        for (long i : friends) {
            if (i == userId) {
                btSend.setVisibility(View.GONE);
                ivDelete.setVisibility(View.VISIBLE);
                return;
            }
        }
    }

    private void setRequestInfo(String reason) {
        JsonTeamRequestReason jtrr = new Gson().fromJson(reason, JsonTeamRequestReason.class);
        TextView tvPhone = findViewById(R.id.tv_phone);
        tvPhone.setText(jtrr.getPhone());
        TextView tvQQ = findViewById(R.id.tv_qq);
        tvQQ.setText(jtrr.getQq());
        findViewById(R.id.ll_phone).setVisibility(View.VISIBLE);
        findViewById(R.id.ll_qq).setVisibility(View.VISIBLE);
        setUserInfo(jtrr.getUser());
    }

    private void setPrivateInfo(char[] isPublic) {
        if (isPublic[8] == '0') {
            tvSchool.setVisibility(View.GONE);
        }
        if (isPublic[7] == '0') {
            tvNo.setVisibility(View.GONE);
        }
        if (isPublic[6] == '0') {
            tvName.setVisibility(View.GONE);
        }
        if (isPublic[5] == '0') {
            tvGender.setVisibility(View.GONE);
        }
        if (isPublic[4] == '0') {
            tvBirthday.setVisibility(View.GONE);
        }
        if (isPublic[3] == '0') {
            tvAcademy.setVisibility(View.GONE);
        }
        if (isPublic[2] == '0') {
            tvMajor.setVisibility(View.GONE);
        }
        if (isPublic[1] == '0') {
            tvGrade.setVisibility(View.GONE);
        }
        if (isPublic[0] == '0') {
            tvClass.setVisibility(View.GONE);
        }
    }
}
